package descarte.com.br.descarte.Dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import descarte.com.br.descarte.Bean.Coletor;
import descarte.com.br.descarte.Bean.Fornecedor;
import descarte.com.br.descarte.Bean.Requisitar;
import descarte.com.br.descarte.Bean.Residuo;

/**
 * Created by jvba on 22/03/2015.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "descart.db";
    private static final int DATABASE_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            Log.i(DatabaseHelper.class.getName(), "onCreate");
            TableUtils.createTable(connectionSource, Coletor.class);
            TableUtils.createTable(connectionSource, Fornecedor.class);
            TableUtils.createTable(connectionSource, Requisitar.class);
            TableUtils.createTable(connectionSource, Residuo.class);
        } catch (Exception e) {
            Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            Log.i(DatabaseHelper.class.getName(), "onUpgrade");
            TableUtils.dropTable(connectionSource, Coletor.class,true);
            TableUtils.dropTable(connectionSource, Fornecedor.class,true);
            TableUtils.dropTable(connectionSource, Requisitar.class,true);
            TableUtils.dropTable(connectionSource, Residuo.class,true);
            onCreate(db, connectionSource);
        } catch (Exception e) {
            Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void close() {
        super.close();
    }
}
